class AnalyticsHelper {
  constructor() {
    this.initGA();

    // 发送第一次打开事件
    gtag({
        type: ANALYTICS_EVENTS.FIRST_OPEN
    });
  }

  initGA() {
    window.dataLayer = window.dataLayer || [];
    window.gtag = (args) => {
      chrome.runtime.sendMessage({
        type: 'GA_EVENT',
        data: args
      });
      window.dataLayer.push(args);
    };
    
    // gtag('js', new Date());
    // gtag('config', GA_TRACKING_ID);
  }

  trackPageView() {
    gtag({
        type: ANALYTICS_EVENTS.PAGE_VIEW,
        params: {
            page_title: window.document.title,
            page_path: window.location.href
        }
    });
  }

  trackScroll() {
    gtag({
        type: ANALYTICS_EVENTS.SCROLL,
        params: {
            page_title: window.document.title,
            page_path: window.location.href
        }
    });
  }

  trackOutboundClick(url) {
    gtag({
        type: ANALYTICS_EVENTS.OUTBOUND_CLICK,
        params: {
            page_title: window.document.title,
            page_path: window.location.href,
            destination: url
        }
    });
  }

  trackSiteSearch(searchTerm) {
    gtag({
        type: ANALYTICS_EVENTS.SITE_SEARCH,
        params: {
            page_title: window.document.title,
            page_path: window.location.href,
            search_term: searchTerm
        }
    });
  }

  trackFormInteraction(formId) {
    gtag({
        type: ANALYTICS_EVENTS.FORM_INTERACTION,
        params: {
            page_title: window.document.title,
            page_path: window.location.href,
            form_id: formId
        }
    });
  }

  trackVideoInteraction(action, videoId) {
    gtag({
        type: ANALYTICS_EVENTS.VIDEO_INTERACTION,
        params: {
            page_title: window.document.title,
            page_path: window.location.href,
            video_id: videoId,
            action: action
        }
    });
  }

  trackFileDownload(fileName) {
    gtag({
        type: ANALYTICS_EVENTS.FILE_DOWNLOAD,
        params: {
            page_title: window.document.title,
            page_path: window.location.href,
            file_name: fileName
        }
    });
  }
}